package com.takepoints21.mobile;

import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.BaseRequestListener;
import com.facebook.android.SessionStore;

import com.takepoints.android.TPConst;
import com.takepoints21.mobile.zxing.CaptureActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

public abstract class BaseActivity extends Activity {
	
	protected SharedPreferences sharePref = null;
	
	protected final String IMG_CACHE_PATH = "/sdcard/takepoints/cache/";
	
	protected void initSharedPreference(){
		if(sharePref == null){
			sharePref = PreferenceManager.getDefaultSharedPreferences(this);
		}
	}
	
	protected int getSharedIntValue(String key){
		if(sharePref == null){
			sharePref = PreferenceManager.getDefaultSharedPreferences(this);
		}
		return sharePref.getInt(key, 0);
	}
	
	protected String getSharedStringValue(String key){
		if(sharePref == null){
			sharePref = PreferenceManager.getDefaultSharedPreferences(this);
		}
		return sharePref.getString(key, TPConst.BLANK);
	}
	
	/*protected int getScreenHeight(){
		DisplayMetrics metrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metrics);
		return metrics.heightPixels;
	}*/
	
	protected void alert(String msg){
		AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
		alertbox.setMessage(msg);
		alertbox.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dlgInf, int which) {
				dlgInf.dismiss();
			}
		});
		alertbox.show();
    }
	
	protected void popup(String msg){
    	Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }
	
	protected void manageMenu(int indx){
		ImageView btnHome = (ImageView)findViewById(R.id.btnHome);
		ImageView btnDiscount = (ImageView)findViewById(R.id.btnDiscount);
		ImageView btnCollect = (ImageView)findViewById(R.id.btnCollect);
		ImageView btnMyReward = (ImageView)findViewById(R.id.btnMyReward);
		ImageView btnTakeIt = (ImageView)findViewById(R.id.btnTakeIt);
		
		if(indx == 1){
			btnHome.setImageResource(R.drawable.bottom13);
		} else{
			btnHome.setImageResource(R.drawable.bottom11);
			btnHome.setOnClickListener(new View.OnClickListener(){
				public void onClick(View view) {
					Intent intent = new Intent(view.getContext(), Takepoints.class);
					startActivity(intent);
					finish();
				 }
			});
		}
		
		if(indx == 2){
			btnDiscount.setImageResource(R.drawable.bottom23);
		} else{
			btnDiscount.setImageResource(R.drawable.bottom21);
		}
		btnDiscount.setOnClickListener(new View.OnClickListener(){
			public void onClick(View view) {
				if(getSharedIntValue(TPConst.PRF_MEMBER_ID) < 1){
					confirmLoginDlg();
				} else{
					Intent intent = new Intent(view.getContext(), MyOffers.class);
					intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
					startActivity(intent);
					finish();
				}
			 }
		});
		
		if(indx == 3){
			btnCollect.setImageResource(R.drawable.bottom33);
		} else{
			btnCollect.setImageResource(R.drawable.bottom31);
		}
		btnCollect.setOnClickListener(new View.OnClickListener(){
			public void onClick(View view) {
				if(getSharedIntValue(TPConst.PRF_MEMBER_ID) < 1){
					confirmLoginDlg();
				} else{
					Intent intent = new Intent(view.getContext(), MyCards.class);
					intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
					startActivity(intent);
					finish();
				}
			}
		});
		
		if(indx == 4){
			btnMyReward.setImageResource(R.drawable.bottom43);
		} else{
			btnMyReward.setImageResource(R.drawable.bottom41);
		}
		btnMyReward.setOnClickListener(new View.OnClickListener(){
			public void onClick(View view) {
				if(getSharedIntValue(TPConst.PRF_MEMBER_ID) < 1){
					confirmLoginDlg();
				} else{
					Intent intent = new Intent(view.getContext(), MyRewards.class);
					intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
					startActivity(intent);
					finish();
				}
			 }
		});
		
		if(indx == 5){
			btnTakeIt.setImageResource(R.drawable.btm_takeit_h);
		} else{
			btnTakeIt.setImageResource(R.drawable.btm_takeit);
		}
		btnTakeIt.setOnClickListener(new View.OnClickListener(){
			public void onClick(View view) {
				if(getSharedIntValue(TPConst.PRF_MEMBER_ID) < 1){
					confirmLoginDlg();
				} else{
					Intent intent = new Intent(view.getContext(), CaptureActivity.class);
					intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
					intent.putExtra("pageSrc", "TakeNow");
					startActivity(intent);
					finish();
				}
			 }
		});
	}
	
	protected void confirmLoginDlg(){
		AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
		alertbox.setMessage("คุณยังไม่ได้ Login !\n\nคุณต้องการที่จะ Login ใช่มั้ย ?");
		alertbox.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dlgInf, int which) {
				gotoLoginScreen();
			}
		});
		alertbox.setNeutralButton("No", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dlgInf, int which) {
				dlgInf.dismiss();
			}
		});
		alertbox.show();
    }
	
	final void gotoLoginScreen(){
		try{
			Intent intent = new Intent(this, UserLogin.class);
			intent.putExtra("pageSrc", "HOME_SCREEN");
			startActivity(intent);
			finish();
		} catch(Exception e){
			popup(e.getMessage());
		}
	}
	
	private com.facebook.android.Facebook facebook;
	private ProgressDialog fbProgress;
	private Handler fbRunOnUi = new Handler();
	
	protected boolean isAuthenFB(){
		facebook = new com.facebook.android.Facebook(FacebookApps.APP_ID);
		SessionStore.restore(facebook, this);
		return facebook.isSessionValid();
	}
	
	protected void postToFacebook(String postMsg, String itemName, String infoURL, String shortDesc, String imageURL) {
		//facebook = new com.facebook.android.Facebook(FacebookApps.APP_ID);
		//SessionStore.restore(facebook, this);
		//if(facebook.isSessionValid()) {
			fbProgress = new ProgressDialog(this);
			fbProgress.setMessage("Posting ...");
			fbProgress.show();
	
			Bundle params = new Bundle();
			params.putString("message", postMsg);
			params.putString("name", itemName);
			params.putString("caption", "Takepoints.com");
			params.putString("link", infoURL);
			params.putString("description", shortDesc);
			params.putString("picture", imageURL);

			AsyncFacebookRunner mAsyncFbRunner = new AsyncFacebookRunner(facebook);
			mAsyncFbRunner.request("me/feed", params, "POST", new BaseRequestListener(){
				@Override
				public void onComplete(String response, Object state) {
					fbRunOnUi.post(new Runnable() {
						@Override
						public void run() {
							fbProgress.cancel();
							popup("โพสต์ไปยัง Facebook แล้ว");
						}
					});
				}
			});
		//}
	}
	
}
